package com.simple.trade.service.vm.impl;

import com.google.common.base.Preconditions;
import com.simple.common.entity.ReqContext;
import com.simple.common.entity.ResData;
import com.simple.common.entity.constant.Payment;
import com.simple.common.entity.dto.pay.vm.RedirectPayDto;
import com.simple.common.entity.dto.pay.vm.RedirectPayRspDto;
import com.simple.common.entity.dto.pay.vm.VmBaseDto;
import com.simple.common.entity.po.sys.ChannelComp;
import com.simple.common.entity.po.sys.Product;
import com.simple.common.entity.po.sys.Trade;
import com.simple.common.entity.req.vm.pay.PayBase;
import com.simple.common.entity.req.vm.pay.RedirectPayReqV2;
import com.simple.common.entity.vo.pay.RedirectResV2;
import com.simple.trade.service.vm.RedirectPayV2Service;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;

/**
 * @author yasud
 * @date 2021年03月11日 11:02
 * @description
 */
@Service
public class RedirectPayV2ServiceImpl extends PayBaseService<RedirectResV2, RedirectPayRspDto> implements RedirectPayV2Service {

    @Value("${simple.publicKey}")
    String publicKey;

    /**
     * 跳转支付
     *
     * @param req
     * @return
     */
    @Override
    public ResData<RedirectResV2> redirectPayV2(RedirectPayReqV2 req) {
        return pay(req, trade -> {
            trade.setGoodsList(req.goodsListJson());
            trade.setBillingInfo(req.billingInfoJson());
            trade.setShippingInfo(req.shippingInfoJson());
            trade.setSyncUrl(req.getRedirectUrl());
        }, Payment.PAY_TYPE.VM_REDIRECT_PAY_V2);
    }

    @Override
    protected VmBaseDto getDto(Trade trade, ReqContext reqContext, PayBase payBase, ChannelComp channel, Product product) {
        RedirectPayDto redirectPayDto = RedirectPayDto.generateDto(trade, reqContext, payBase.getExtraInfo(), channel, product);
        return redirectPayDto;
    }

    @SneakyThrows
    @Override
    protected RedirectResV2 map(RedirectPayRspDto rsp) {
        Trade trade = rsp.getTrade();
        Preconditions.checkNotNull(trade, "支付失败渠道服务错误，交易不能为空");
        RedirectResV2 redirectResV2 = new RedirectResV2();
        redirectResV2.setSerialNo(trade.getId());
        redirectResV2.setOrderNo(trade.getMerchantOrderNo());
        redirectResV2.setCurrency(trade.getCurrency());
        redirectResV2.setAmount(trade.getAmount());
        redirectResV2.setPayStatus(trade.getStatus());
        redirectResV2.setRemark(trade.getOrderDesc());
        redirectResV2.setPayMsg(trade.getMsg());
        if (StringUtils.isNotBlank(rsp.getPayHtml())) {
            redirectResV2.setPayHtml(URLEncoder.encode(rsp.getPayHtml(), "UTF-8"));
        }
        else {
            redirectResV2.setPayHtml("");
        }
        if (StringUtils.isNotBlank(rsp.getPaymentUrl())) {
            redirectResV2.setPayUrl(URLEncoder.encode(rsp.getPaymentUrl(), "UTF-8"));
        }
        else {
            redirectResV2.setPayUrl("");
        }
        return redirectResV2;
    }
}
